﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RainEngine.Controllers;
using RainEngine.SceneManagement;

namespace RainEngine.SceneNodes.UI
{
    /// <summary>
    /// 文字对齐方式枚举
    /// </summary>
    public enum TextAlign
    {
        MiddleLeft,
        MiddleCenter
    }
    
    /// <summary>
    /// 按钮控件
    /// </summary>
    public class UIButton:UIButtonBase 
    {
        #region 成员变量和构造函数
        
        //默认值
        private static readonly Vector2  DefaultButtonSize = new Vector2 (116,38);
        private static readonly Rectangle  DefaultRectSource=new Rectangle (0, 0, 64, 24);
        private static readonly Rectangle  DefaultRectHighlight=new Rectangle (64, 0, 64, 24);
        private static readonly Rectangle DefaulRectClicked = new Rectangle(64, 24, 64, 24);
        private static readonly Color DefaultTextColor=Color .White ;
        private static readonly Color DefaultTextHighlight=Color.Red ;
        
        /// <summary>
        /// 文字的对齐方式，默认为居中对齐
        /// </summary>
        protected TextAlign textAlign = TextAlign.MiddleCenter;
        
        /// <summary>
        /// 鼠标按下时纹理的源矩形
        /// </summary>
        protected Rectangle rectSourceClicked;

        /// <summary>
        /// 创建一个默认按钮
        /// </summary>
        /// <param name="engine">引擎</param>
        /// <param name="setScene">所属场景</param>
        public UIButton(RainGE engine, Scene setScene): this(engine, setScene, Vector2.Zero, "UIButton", DefaultButtonSize, "Button", engine.DefaultFont, DefaultTextColor, DefaultTextHighlight, DefaultRectSource, DefaultRectHighlight, DefaulRectClicked,true )
        {
            
        }

        /// <summary>
        /// 使用自定义的背景创建一个按钮，你也可以将纹理名称设为null创建一个只有文字的按钮。
        /// </summary>
        /// <param name="engine">引擎</param>
        /// <param name="setScene">所属Scene</param>
        /// <param name="setTextureName">纹理名称</param>
        public UIButton(RainGE engine, Scene setScene, string setTextureName)
            : this(engine, setScene, Vector2.Zero, setTextureName, DefaultButtonSize, "Button", engine.DefaultFont, DefaultTextColor, DefaultTextHighlight, DefaultRectSource, DefaultRectHighlight, DefaulRectClicked,false )
        {
 
        }

        /// <summary>
        /// 创建一个新UIButton对象，内部方法，只能在引擎内部调用。
        /// </summary>
        /// <param name="engine">引擎</param>
        /// <param name="setScene">此控件所属的scene</param>
        /// <param name="setPosition">2D屏幕位置</param>
        /// <param name="setTextureName">包含路径的背景纹理的名称</param>
        /// <param name="setSize">2D屏幕大小</param>
        /// <param name="setText">按钮上的文字</param>
        /// <param name="setFont">字体</param>
        /// <param name="setTextColorOrigin">文字原始颜色</param>
        /// <param name="setTextColorHighlight">高亮状态的文字颜色</param>     
        /// <param name="setRectOrigin">普通状态图像源矩形</param>
        /// <param name="setRectHighlight">高亮状态的图像源矩形</param>
        /// <param name="setRectClicked">鼠标点击状态的图像源矩形</param>           
        internal UIButton(RainGE engine, Scene setScene, Vector2 setPosition, string setTextureName, Vector2 setSize, string setText, SpriteFont setFont, Color setTextColor, Color setTextColorHighlight, Rectangle setRectOrigin, Rectangle setRectHighlight, Rectangle setRectClicked,bool useResource)
            : base(engine, setScene, setPosition, setTextureName, setSize, setText, setFont, setTextColor, setTextColorHighlight, setRectOrigin, setRectHighlight,useResource )
        {            
            this.rectSourceClicked = setRectClicked;
            //如果不包含背景图片，则按钮的大小就是文字的大小
            if (string.IsNullOrEmpty(TextureName))
                this.size = new Vector2(setFont.MeasureString(setText).X, setFont.MeasureString(setText).Y);

            this.TextChanged += new System.EventHandler(UIButton_TextChanged);
        }

        void UIButton_TextChanged(object sender, System.EventArgs e)
        {
            //如果不包含背景图片，则按钮的大小就是文字的大小
            if (string.IsNullOrEmpty (TextureName))
                this.size = new Vector2(font.MeasureString(text).X, font.MeasureString(text).Y);
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取或设置文字的对齐方式
        /// </summary>
        public TextAlign TextAlign
        {
            get { return textAlign; }
            set { textAlign = value; }
        }

        /// <summary>
        /// 获取或设置鼠标按下时纹理的源矩形
        /// </summary>
        public Rectangle RectSourceClicked
        {
            get { return rectSourceClicked; }
            set { rectSourceClicked = value; }
        }        
        
        #endregion

        /// <summary>
        /// 绘制按钮
        /// </summary>
        public override int Draw(GameTime gameTime)
        {

            //如果按钮背景图片不为null，则绘制背景
            if (TextureName != null)
            {
                //获取图像淡入淡出的透明颜色
                alphaTextureColor = new Color(Color.R, Color.G, Color.B, Scene.TransitionAlpha);

                //根据鼠标移入还是按下设置图像源矩阵
                Rectangle rect = isMouseInside || (uiManager.ActiveControl == this) ? ((Input.MouseLeftButtonPressed && isMouseInside) ? rectSourceClicked : rectSourceHighlight) : rectSourceOrigin;
                //绘制按钮背景图像
                RainGE.SpriteBatch.Draw(texture, new Rectangle((int)Position.X, (int)Position.Y, (int)size.X, (int)size.Y), rect, alphaTextureColor, Rotation, Origin, SpriteEffect, LayerDepth);
            }

            //如果按钮文字不为null，则绘制文字
            if (text != null)
            {
                Color textColor = isMouseInside || (uiManager.ActiveControl == this) ? textColorHighlight : TextColor;
                // 设置淡入淡出的透明度
                Color alphaTextColor = new Color(textColor.R, textColor.G, textColor.B, Scene.TransitionAlpha);

                //如果文字的对齐方式为MiddleCenter
                if (textAlign == TextAlign.MiddleCenter)
                    RainGE.SpriteBatch.DrawString(font, text, new Vector2(Position.X + size.X / 2 - font.MeasureString(text).X / 2, Position.Y + size.Y / 2 - font.MeasureString(text).Y / 2), alphaTextColor, Rotation, Origin, 1.0f, SpriteEffect, LayerDepth);
                //如果文字的对齐方式为MiddleLeft
                else if (textAlign == TextAlign.MiddleLeft)
                    RainGE.SpriteBatch.DrawString(font, text, new Vector2(Position.X, Position.Y + size.Y / 2 - font.MeasureString(text).Y / 2), alphaTextColor, Rotation, Origin, 1.0f, SpriteEffect, LayerDepth);
                
            }
            return 1;
        }
    }
}
